package com.zhao.shiyuanbaoyou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhao.shiyuanbaoyou.R;
import com.zhao.shiyuanbaoyou.adapter.HomeAdapter;
import com.zhao.shiyuanbaoyou.adapter.TenAdapter;
import com.zhao.shiyuanbaoyou.bean.TenBean;
import com.zhao.shiyuanbaoyou.bean.ViewPagerBean;
import com.zhao.shiyuanbaoyou.interfacepackage.IViewPager;
import com.zhao.shiyuanbaoyou.urlutil.Constant;
import com.zhao.shiyuanbaoyou.util.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 张昭 on 2016/11/12.
 */

public class TenFragmentItem extends Fragment {
    private RecyclerView recyclerView_tens;
    private Context mContext;
    private List<TenBean.RowsBean> mList = new ArrayList<>();
    private TenAdapter mAdapter;
    private int index = 0;
    private Observable<TenBean> observable;

    public TenFragmentItem(int index) {
        this.index = index;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_fragment_ten, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_tens = (RecyclerView) view.findViewById(R.id.recyclerView_fragment_tenss);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initGetData();
    }

    private void initGetData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.Ten_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        IViewPager iViewPager = retrofit.create(IViewPager.class);
        switch (index) {
            case 0:
                observable = iViewPager.getJsonTen1String();
                break;
            case 1:
                observable = iViewPager.getJsonTen2String();
                break;
            case 2:
                observable = iViewPager.getJsonTen3String();
                break;
            case 3:
                observable = iViewPager.getJsonTen4String();
                break;
        }
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<TenBean>() {
                    @Override
                    public void call(TenBean tenBean) {
                        mList = tenBean.getRows();
                        Log.i("TAG", "-------->>>>>>>>>>call: " + mList.get(1).getName());
                        recyclerView_tens.setLayoutManager(new GridLayoutManager(mContext, 2));
                        recyclerView_tens.setHasFixedSize(true);
                        recyclerView_tens.addItemDecoration(new SpacesItemDecoration(3));
                        mAdapter = new TenAdapter(mContext, mList);
                        recyclerView_tens.setAdapter(mAdapter);
                    }
                });
    }

}
